 package com.medicine.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ValueChangeEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import com.medicine.model.DetailCategory;
import com.medicine.model.IllnessCategory;
import com.medicine.model.SubCategory;

@ViewScoped
@Named
public class KaiFangZiController implements Serializable{

	private static final long serialVersionUID = -6729273992719187266L;
	
	@Inject DetailCategory detailCategory;
	@PersistenceContext EntityManager em;
	@Inject UserTransaction ut;
	
	private List<IllnessCategory> illNessCategoryList;
	private List<SubCategory> subCategoryList;
	private List<DetailCategory> detailCategoryList;
	private int selectedScType;
	private String mainSolution;
	private String cateOneName;
	private String cateTwoName;
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void postConstruct(){
		illNessCategoryList = em.createQuery("select cc from IllnessCategory cc").getResultList();
		if (illNessCategoryList != null && illNessCategoryList.size() != 0){
			try {
				loadSubCategoryList(illNessCategoryList.get(0).getId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void onSelectedValueChanged(ValueChangeEvent vcEvent){
		try {
			loadSubCategoryList((int)vcEvent.getNewValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void onScValueChanged(ValueChangeEvent vcEvent){
		if (subCategoryList != null){
			for (SubCategory sc: subCategoryList){
				if (sc.getId() == (int)vcEvent.getNewValue()){
					try {
						ut.begin();
						em.joinTransaction();
						em.refresh(em.merge(sc));
						ut.commit();
					} catch (Exception e) {
						e.printStackTrace();
					}
					detailCategoryList = sc.getDetailCategories();
				}
			}
		}
	}
	
	private void loadSubCategoryList(int seletectedIllnessCategoryId) throws Exception{
		List<?> list = em.createQuery("select ic from IllnessCategory ic where ic.id = ?1").setParameter(1, seletectedIllnessCategoryId).getResultList();
		if (list != null && list.size() != 0){
			IllnessCategory ic = (IllnessCategory)list.get(0);
			
			ut.begin();
			em.joinTransaction();
			
			IllnessCategory mergedIC = em.merge(ic);
			em.refresh(mergedIC);
			
			ut.commit();
			
			ut.begin();
			em.joinTransaction();
			
			subCategoryList = em.merge(ic).getSubCategories();
			
			ut.commit();
			
			if (subCategoryList != null && subCategoryList.size() != 0){
				if (detailCategoryList == null){
					detailCategoryList = subCategoryList.get(0).getDetailCategories();
				}
			}
		}
	}
	
	public void generateSolution(){
		for (SubCategory sc: subCategoryList){
			if (sc.getId() == selectedScType){
				mainSolution = sc.getMainSolution();
				setCateTwoName(sc.getSubCateName());
				setCateOneName(sc.getIllnessCategory().getIllnessName());
				break;
			}
		}
	}

	public String getMainSolution() {
		return mainSolution;
	}

	public void setMainSolution(String mainSolution) {
		this.mainSolution = mainSolution;
	}

	public DetailCategory getDetailCategory() {
		return detailCategory;
	}

	public void setDetailCategory(DetailCategory subCategory) {
		this.detailCategory = subCategory;
	}

	public List<IllnessCategory> getIllNessCategoryList() {
		return illNessCategoryList;
	}

	public void setIllNessCategoryList(List<IllnessCategory> illNessCategoryList) {
		this.illNessCategoryList = illNessCategoryList;
	}

	public List<SubCategory> getSubCategoryList() {
		return subCategoryList;
	}

	public void setSubCategoryList(List<SubCategory> subCategoryList) {
		this.subCategoryList = subCategoryList;
	}

	public List<DetailCategory> getDetailCategoryList() {
		return detailCategoryList;
	}

	public void setDetailCategoryList(List<DetailCategory> detailCategoryList) {
		this.detailCategoryList = detailCategoryList;
	}

	public int getSelectedScType() {
		return selectedScType;
	}

	public void setSelectedScType(int selectedScType) {
		this.selectedScType = selectedScType;
	}

	public String getCateOneName() {
		return cateOneName;
	}

	public void setCateOneName(String cateOneName) {
		this.cateOneName = cateOneName;
	}

	public String getCateTwoName() {
		return cateTwoName;
	}

	public void setCateTwoName(String cateTwoName) {
		this.cateTwoName = cateTwoName;
	}

}
